package com.rongji.cms.tools.temp.db;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.rongji.cms.tools.temp.db.entity.DiffColumnVo;
import com.rongji.cms.tools.temp.db.entity.LackColumnVo;
import com.rongji.cms.tools.temp.db.entity.LackTableVo;
import com.rongji.cms.tools.temp.db.entity.MysqlColumnProp;
import com.rongji.cms.tools.temp.db.utils.CompareUtil;
import com.rongji.cms.tools.temp.db.utils.MySQLUtil;

import java.io.*;
import java.util.Map;

public class TestMysql {
    // info of database A
    private final static String A_HOST = "192.168.212.224:3306";
    private final static String A_DBNAME = "test1";
    private final static String A_USERNAME = "root";
    private final static String A_USERPWD = "123456";
    // info of database B
    private final static String B_HOST = "192.168.212.224:3306";
    private final static String B_DBNAME = "test2";
    private final static String B_USERNAME = "root";
    private final static String B_USERPWD = "123456";
    //         不需要对比的 在类ColumnProp里直接加注解 @CompareIgnore就好
    // help you define which field u want to ignore in cn.lxw.mysql.entity.MysqlColumnProp
//    public static volatile Map<String, String> ignoreFieldNameMap = new ConcurrentHashMap<>();

    static String outTxtFile = "C:\\compareResult.txt";
    static String outExcelFile = "C:\\compareResult.xls";

    /**
     * 功能描述: <br>
     * 〈Start Here〉
     *
     * @Param: [args]
     * @Return: void
     * @Author: luoxw
     * @Date: 2021/6/4 15:10
     */
    public static void main(String[] args) throws Exception {

//        ignoreMap.put("ordinalPosition","ordinalPosition");
        Map<String, Map<String, MysqlColumnProp>> dbAInfo = MySQLUtil.getDBInfo(A_HOST, A_DBNAME, A_USERNAME, A_USERPWD);
        Map<String, Map<String, MysqlColumnProp>> dbBInfo = MySQLUtil.getDBInfo(B_HOST, B_DBNAME, B_USERNAME, B_USERPWD);

        String compareResult = CompareUtil.compareDBInfo(dbAInfo, dbBInfo);
        System.out.println(compareResult);

        writeToExcel();
    }

    /**
     * 将对比结果导出到excel
     * 3个sheet写入到同一个excel文件
     */
    public static void writeToExcel() {
        //1、创建一个文件对象
        File excelFile = new File(outExcelFile);
        //2、判断文件是否存在，不存在则创建一个Excel文件
        if (!excelFile.exists()) {
            try {
                excelFile.createNewFile();//创建一个新的文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        ExcelWriter excelWriter = null;
        try {
            OutputStream os = new FileOutputStream(excelFile);
            excelWriter = EasyExcel.write(os).build();

//            缺少的表
            // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样
            WriteSheet lackTableSheet = EasyExcel.writerSheet(0, "缺少的表").head(LackTableVo.class).build();
            excelWriter.write(CompareUtil.lackTableVoList, lackTableSheet);
//            缺少的字段
            WriteSheet lackColumnSheet = EasyExcel.writerSheet(1, "缺少的字段").head(LackColumnVo.class).build();
            excelWriter.write(CompareUtil.lackColumnVoList, lackColumnSheet);
//            差异的字段
            WriteSheet diffColumnSheet = EasyExcel.writerSheet(2, "有差异的字段").head(DiffColumnVo.class).build();
            excelWriter.write(CompareUtil.diffColumnVoList, diffColumnSheet);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }

            System.out.println("excel导出完成。。。。");
        }
    }


    /**
     * 将对比结果写入到txt
     *
     * @param compareResult
     */
    public static void writeToTxt(String compareResult) {
        try {
            //1、创建一个文件对象
            File txtFile = new File(outTxtFile);
            //2、判断文件是否存在，不存在则创建一个Excel文件
            if (!txtFile.exists()) {
                try {
                    txtFile.createNewFile();//创建一个新的文件
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(txtFile));

            out.write(compareResult);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("txt导出完成。。。。");
    }

}
